package com.jckl.foodathon.restlet.services;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jckl.foodathon.common.constant.ApplicationConstants;
import com.jckl.foodathon.common.util.GsonUtil;
import com.jckl.foodathon.dao.BlogDAO;
import com.jckl.foodathon.model.Blog;
import com.jckl.foodathon.model.Response;


/**
 * Resource which has only one representation.
 * 
 */
public class BlogResource extends ServerResource {
	
	private static final Logger log = Logger.getLogger(BlogResource.class.getName());


    @Post("json")
    public String addBlog(Representation entity) {
    	
    	Response response = new Response();
    	response.setServerResponse(ApplicationConstants.SERVER_RESP_FAIL);
    	
    	Gson gson = GsonUtil.deserializeGson();
    	
    	boolean validationPass = true;
    	
    	try{
    		JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
    		log.info("Incoming JSON: "+req.toString());
    		
    		Blog blog = gson.fromJson(req.toString(), Blog.class);
    		
    		response.setStatus(req.toString());
    		
    		//Upkeep blog info
    		blog.setActive(true);
    		blog.setCreatedDate(new Date());
    		blog.setUpdatedDate(new Date());
    		
    		//Add blog into datasource
    		BlogDAO blogDao = new BlogDAO();
    		
    		//Validate brand name
    		if(blogDao.getBlogByURL(blog.getBlogURL())!=null){
    			response.setServerResponse(ApplicationConstants.SERVER_RESP_FAIL);
    			response.setRemark("Existing blog entry found!");
    			validationPass = false;
    		}
    		
    		if(validationPass){
        		if(blogDao.newBlog(blog)){
        			response.setServerResponse(ApplicationConstants.SERVER_RESP_SUCCESS);
            		log.info("Blog '"+blog.getTitle()+"' successfully added into datastore");
        		}else{
        			log.severe("Blog '"+blog.getTitle()+"' unable to add into datastore");
        		}
    		}else{
    			log.severe("Blog '"+blog.getTitle()+"' already exist in the system");
    		}

    	}catch(JsonSyntaxException jsonSyntaxEx){
    		jsonSyntaxEx.printStackTrace();
    	}catch(JSONException jsonEx){
    		jsonEx.printStackTrace();
    	}catch(IOException ioEx){
    		ioEx.printStackTrace();
    	}
		return gson.toJson(response);
    }
    
    @Get
    public String getBlogs() {
    	
    	BlogDAO blogDao = new BlogDAO();
    	List<Blog> blogList = blogDao.getBlogs();
    	
    	String blogJsonArray = new String();
    	Gson gson = new Gson();
    	blogJsonArray = gson.toJson(blogList);
    	
        return blogJsonArray;
    }
    
    @Put("json")
    public String updateBlog(Representation entity) {

    	Response response = new Response();
    	response.setServerResponse(ApplicationConstants.SERVER_RESP_FAIL);
    	Gson gson = new Gson();
    	
    	try{
    		JSONObject req = (new JsonRepresentation(entity)).getJsonObject();
    		
    		Blog blog = gson.fromJson(req.toString(), Blog.class);
    		
			//Setting the system data
    		blog.setUpdatedDate(new Date());
    		
    		//Add store type into datasource
    		BlogDAO blogDao = new BlogDAO();
    		
    		if(blogDao.updateBlog(blog)){
    			//Update blog successfully
    			response.setServerResponse(ApplicationConstants.SERVER_RESP_SUCCESS);
        		log.info("Blog '"+blog.getTitle()+"' successfully updated into datastore");
    		}else{
    			log.severe("Blog '"+blog.getTitle()+"' unable to update into datastore");
    		}
    		
    	}catch(JSONException jsonEx){
    		jsonEx.printStackTrace();
    	}catch(IOException ioEx){
    		ioEx.printStackTrace();
    	}
    	return gson.toJson(response);
    }
    
}
